package leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * ClassName: ShuenfT1
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/7 09:57
 * @Version 1.0
 */
public class ShuenfT1_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            int n = scanner.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }
            for (int i = 0; i < n; i++) {
                b[i] = scanner.nextInt();
            }

            int[] pos = new int[n + 1];
            for (int i = 0; i < n; i++) {
                pos[a[i]] = i;
            }

            int count = 0;
            int minIndex = Integer.MAX_VALUE;
            for (int i = n - 1; i >= 0; i--) {
                if (pos[b[i]] < minIndex) {
                    count++;
                    minIndex = pos[b[i]];
                } else {
                    break;
                }
            }
            System.out.println(n - count);
        }
    }
}
